<template>
    <div :class="{ hidden: hidden }" class="pagination-container">
        <el-pagination :background="background" v-model:current-page="currentPage" v-model:page-size="pageSize"
            :layout="layout" :page-sizes="pageSizes" :pager-count="pagerCount" :total="total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
</template>
<script setup lang="ts">
import { defineProps, defineEmits, computed } from 'vue'

const props = defineProps({
    total: {
        required: true,
        type: Number 
    },
    page: {
        type: Number,
        default: 1
    },
    limit: {
        type: Number,
        default: 10
    },
    pageSizes: {
        type: Array,
        default() {
            return [10, 20, 30, 50]
        }
    },
    // 移动端页码按钮的数量端默认值5
    pagerCount: {
        type: Number,
        default: document.body.clientWidth < 992 ? 5 : 7
    },
    layout: {
        type: String,
        default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
        type: Boolean,
        default: true
    },
    autoScroll: {
        type: Boolean,
        default: true
    },
    hidden: {
        type: Boolean,
        default: false
    }
})

const emit = defineEmits(['update:limit', 'update:page', 'pagination'])
const currentPage = computed({
    get() {
        return props.page
    },
    set(val) {
        emit('update:page', val)
    }
})
const pageSize = computed({
    get() {
        return props.limit
    },
    set(val) {
        emit('update:limit', val)
    }
})

function handleSizeChange(val) {
    if (currentPage.value * val > props.total) {
        currentPage.value = 1
    }
    emit('update:limit', val)
    emit('pagination', { page: currentPage.value, limit: val })
}

function handleCurrentChange(val) {
    emit('update:page', val)
    emit('pagination', { page: val, limit: pageSize.value })
}
</script>

<style scoped lang="scss">
.pagination-container {
    background: #fff;
    display: flex;
    justify-content: flex-end;
    margin-top: 15px;
}

.pagination-container.hidden {
    display: none;
}
</style>